package vendingMachine.gui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;

import vendingMachine.inventory.Item;
import vendingMachine.inventory.VendingInventory;

public class DisplayManager {
    
    public int SHOW_CART   = -1;
    public int CHECK_OUT   = -2;
    public int CANCEL_SALE = -3;
    
    public int DisplayInventory(ShoppingCart shoppingCart) {
        System.out.println("\n\n\n\n\n\n\n\n");
        VendingInventory inventory = VendingInventory.getInstance();
        
        Iterator itemIterator = inventory.getItemList().iterator();

        boolean printItem = false;
        while (itemIterator.hasNext()) {
            Item iItem = (Item)itemIterator.next();
            printItem = true;
            Iterator cartIterator = shoppingCart.createIterator();
            while (cartIterator.hasNext()) {
                ShoppingCartItem cItem = (ShoppingCartItem)cartIterator.next();
                
                if (cItem.getProductID() == iItem.getID()) {
                    if (cItem.getQuantity()
                    >= iItem.getQuantity())
                        printItem = false;
                }
            }
            
            if (printItem) {
                System.out.format("%s. %-30s$%.2f%n", iItem.getID(),
                        iItem.getDescription(),
                        iItem.getPrice());
            }
        }
        
        /* THESE PROMPTS NEED TO BE CLEANED UP
         * Possibly create a method to do output of prompts, passing param to
         * show which menu we're on.
         */
        System.out.format("(S) Show cart      Total = $%.2f%n", shoppingCart.calculateTotal());
        System.out.println("(X) Cancel sale");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = null;
        try {
            input = br.readLine();
        } catch(IOException ioe) {
            System.out.println("Input error!");
            System.exit(1);
        }
        
        int returnVal = 0;
        if (input.trim().toLowerCase().equals("s"))
            returnVal = -1;
        else if (input.trim().toLowerCase().equals("x"))
            returnVal = -3;
        else if (input.trim().toLowerCase().equals("e"))
            System.exit(0);
        else {
            int index = 0;
            try {
                index = Integer.parseInt(input);
            } catch(NumberFormatException nfe) {
                index = -1;
            }
            if (index != -1)
                returnVal = inventory.getItem(index).getID();
            else
                returnVal = -1;
        }
        
        return returnVal;
    }
    
    public int DisplayShoppingCart(ShoppingCart shoppingCart) {
        System.out.println("\n\n\n\n\n\n\n\n");
        Iterator itemIterator = shoppingCart.createIterator();
        
        while (itemIterator.hasNext()) {
            ShoppingCartItem item = (ShoppingCartItem)itemIterator.next();
            System.out.format("%d. %-30sx%-3d$%.2f%n",
                    item.getProductID(),
                    item.getItem().getDescription(),
                    item.getQuantity(),
                    item.getCost());
        }
        System.out.format("%nTotal = $%.2f%n%n", shoppingCart.calculateTotal());
        System.out.println("(C) Check out");
        System.out.println("(S) Continue shopping");
        System.out.println("(X) Cancel sale");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = null;
        try {
            input = br.readLine();
        } catch(IOException ioe) {
            System.out.println("Input error!");
            System.exit(1);
        }
        if (input.trim().toLowerCase().equals("c"))
            return -2;
        else if (input.trim().toLowerCase().equals("s"))
            return -1;
        else if (input.trim().toLowerCase().equals("x"))
            return -3;
        else
            return shoppingCart.getItem(Integer.parseInt(input)).getProductID();
    }
    
}
